<template>
  <div style="width: 1920px">

        <span style="display: block;border: 0px solid red;width: 500px;margin: 20% 100px 300px 7%;">
        <h1>欢迎来到憨宠</h1>
        <h1>一家专门为宠物而生的商城与社区</h1>
        </span>

    <div style="width: 400px;margin-left: 70%;margin-top:-20% ;border: 0px solid red;">
      <h1 style="text-align: center; margin: 20px 0;">欢迎来到憨宠</h1>
      <div style="text-align: center;margin-bottom: 20px;padding: 10px"><span  @click="Show(1)">用户名登录</span>|<span @click="Show(2)">验证码登录</span></div>
      <el-form v-if="isShow" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item style="margin-left: 20px ">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>&nbsp;
          <a href="/reg">还没有账号？去注册</a>
        </el-form-item>
      </el-form>

      <el-form v-if="isShow2" :model="ruleForm1" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="手机号" prop="username">
          <el-input v-model="ruleForm1.phone"></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="ruleForm1.code"  class="input-with-select">
            <el-button slot="append" @click="VerificationCode()" >获取验证码</el-button>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-left: 20px ">
          <el-button type="primary" @click="VerificationCodeLogin('ruleForm')">登录</el-button>&nbsp;
          <a href="/reg">还没有账号？去注册</a>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
export default {
  data() {
    return {
      url: '176.212.26.22:9090',
      isShow: false,
      isShow2: true,
      ruleForm: {
        username: '',
        password: '',
      },
      ruleForm1: {
        phone: '',
        code: '',
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '验证码未输入', trigger: 'blur'},
          {min: 6, max: 6, message: '验证码长度不正确', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    /* 切换登录方式 */
    Show(int) {
      if (int == 1) {
        if (this.isShow != true) {
          this.isShow = true;
          this.isShow2 = false;
        }
      } else if (int == 2) {
        if (this.isShow2 != true) {
          this.isShow2 = true;
          this.isShow = false;
        }
      }
    },
    /* 获取验证码 */
    VerificationCode(){
      let url = 'http://' + this.url + '/user/login/phone'
      console.log('url=' + url)
      this.axios.post(url,this.ruleForm1.phone).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message.success(responseBody.message);

          /*// 测试能否获取jwt
          let localJwt = localStorage.getItem("jwt");
          console.log("从localStorage中读取JWT："+localJwt);
*/
        } else if (responseBody.state == 40100) {
          this.$message.error(responseBody.message)
        }
      })
    },
    /* 验证码登录 */
    VerificationCodeLogin(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://' + this.url + '/user/login/phone'
          console.log('url=' + url)
          /* 将请求参数更改为formData */
          let formData = this.qs.stringify(this.ruleForm1);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message.success("登录成功！");

              // 获取服务器端响应的JWT，并存储到localStorage中
              let jwt = responseBody.data;
              console.log("登录成功,服务器响应的JWT:" + jwt);
              localStorage.setItem("jwt", jwt);
              this.$router.push("/shopping")

              /*// 测试能否获取jwt
              let localJwt = localStorage.getItem("jwt");
              console.log("从localStorage中读取JWT："+localJwt);
*/
            } else if (responseBody.state == 40100) {
              this.$message.error(responseBody.message)
            } else {
              this.$message.error(responseBody.message)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    /* 用户名密码登录 */
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://' + this.url + '/user/login'
          console.log('url=' + url)
          /* 将请求参数更改为formData */
          let formData = this.qs.stringify(this.ruleForm);
          this.axios.post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message.success("登录成功！");

              // 获取服务器端响应的JWT，并存储到localStorage中
              let jwt = responseBody.data;
              console.log("登录成功,服务器响应的JWT:" + jwt);
              localStorage.setItem("jwt", jwt);
              this.$router.push("/shopping")

              /*// 测试能否获取jwt
              let localJwt = localStorage.getItem("jwt");
              console.log("从localStorage中读取JWT："+localJwt);
*/
            } else if (responseBody.state == 40100) {
              this.$message.error(responseBody.message)
            } else {
              this.$message.error(responseBody.message)
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style>
body {
  background-repeat: no-repeat;
  background-size: 1920px;

}
</style>